var tesseract = require('node-tesseract')
var OCRAD = require('./ocrad.js');
var Canvas = require('canvas');
var Image = Canvas.Image;
var fs = require('fs');

/**
 * 识别图片
 * @param imgPath
 * @param options tesseract options
 * @returns {Promise}
 */
function recognizer (imgPath, options) {
    options = options ? options : {psm: 7}

    return new Promise((resolve, reject) => {
        tesseract
            .process(imgPath, options, (err, text) => {
                if (err) return reject(err);
                resolve(text.replace(/[\r\n\s]/gm, ''));
            });
    });
}

var recognizerPureJs = function(imgPath) {
    fs.readFile(imgPath, function(err, src) {
        if (err) {
          throw err;
        }
        var img = new Image();
        img.src = src;
        var canvas = new Canvas(img.width, img.height);
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, img.width, img.height);
        console.log(OCRAD(canvas));
      });
}

exports.recognizer = recognizer
exports.recognizerPureJs = recognizerPureJs